


import wx
import wx.richtext
from pyMarkdownEditorFrm import *
import markdown
import markdown.extensions

#import latex

#from markdown_edit import MarkdownDocument

import wx.html2
import os
import sys

scriptdir = os.path.dirname(os.path.realpath(__file__))

sys.path.append(scriptdir)
MARKDOWN_EXT = ('codehilite','extra','wikilinks','toc','smarty','sane_lists','nl2br',
                'meta','headerid','admonition','smart_strong','tables','footnotes','fenced_code',
                'def_list','attr_list','abbr','latex')
MARKDOWN_CSS = os.path.join(scriptdir, 'styles/markdown.css')
PYGMENTS_CSS = os.path.join(scriptdir, 'styles/pygments.css')

class MarkdownDoc:
    
    def __init__(self, markdown_css=MARKDOWN_CSS, pygments_css=PYGMENTS_CSS ):
        
        self.inline_css = ''

        if markdown_css:
            with open(markdown_css) as markdown_css_file:
                self.inline_css += markdown_css_file.read()

        if pygments_css:
            with open(pygments_css) as pygments_css_file:
                self.inline_css += pygments_css_file.read()
        
        self.md = markdown.Markdown(extensions=MARKDOWN_EXT)
    
    def getHtml(self,text):
        self.md.reset()
        return self.md.convert(text)

    def getHtmlPage(self,text):
        return """<!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <style type="text/css">
        %s
        </style>
        </head>
        <body>
        <div class="markdown-body">
        %s
        </div>
        </body>
        </html>
        """ % (self.inline_css, self.getHtml(text))


class MdView(pyMdEditor):
    def __init__(self,openfilename=''):
        pyMdEditor.__init__(self,None)
      
        self.m_stc.Bind(wx.stc.EVT_STC_MODIFIED, self.OnSTCInput)
        self.m_stc.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER)
        self.m_stc.SetMarginWidth(1, 20)  
        
        self.mdoc = MarkdownDoc()
                
        self.filename=openfilename
        if(self.filename !=''):
            self.m_stc.LoadFile(self.filename)
            self.OnSTCInput(None)                  
        
    def OnSTCInput( self, event ):
        text=self.m_stc.GetValue()
        html=self.mdoc.getHtmlPage(text)
        self.m_browser.SetPage(html,'')
        
    def OnFileOpen( self, event ):
        fname=wx.FileSelector()
        if(fname!=''):
            self.filename=fname
            self.m_stc.LoadFile(self.filename)
    
    def OnFileSave( self, event ):
        if self.filename!='':
            self.m_stc.SaveFile(self.filename)
        else:
            fname=wx.FileSelector()
            if(fname!=''):
                self.filename=fname
                self.m_stc.SaveFile(self.filename)
            
    
    def OnFileSaveAs( self, event ):
        fname=wx.FileSelector()
        if(fname!=''):
            self.filename=fname
            self.m_stc.SaveFile(self.filename)
    
    def OnExit( self, event ):
        event.Skip()
    
    def OnFontSel( self, event ):
        event.Skip()
    
        